const  {users} = require('../models/init-models')();


const account = {
  login:(req, res, next) => {   
    console.log(req.body)  
      const { username, password } = req.body      
     // if(req.session.captcha != verification) res.send("验证码不正确")

      users.findOne({where:{ username, password },raw:true}).then((result)=>{
          if(result){
            console.log(result)
            let data={id:result.id,name:result.username,role:''}
            req.session.user=result.username
            res.status(200).json(data)
            //set({'authorization':req.sessionID})
          }
          else res.status(500).json()
        }).catch((error)=>{
          console.log(error)
        })    
  },
  regist:(req, res, next) => {      
      console.log(req.body)
      users.create(req.body).then((result)=>{
          console.log('注册成功')            
          res.status(200).json(result.dataValues)           
      }).catch((error)=>{
          console.log('注册失败') 
          res.status(500).json()            
      }) 
  },
  info:(req, res, next) => {      
      console.log(req.body)
      users.findByPk(req.body.id).then((result)=>{
          console.log(result.dataValues)          
          res.status(200).json(result.dataValues)
      }).catch((error)=>{
        console.log(error)
        res.status(500).json()
      })    
  },
  edit:(req, res, next) => {     
      console.log(req.body)
      users.update(req.body,{ where: {id: req.body.id }, }).then((result)=>{
          console.log('修改了'+ result + '条记录')
          res.status(200).json()
      }).catch((error)=>{
          console.log('修改失败')
          res.status(500).json()    
      }) 
  }
}



module.exports = account;
